﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Public.Ocean.Petrel.Extensions.Internal
{
    public static class DictionaryExtensions
    {
        public static TValue GetOrAddNew<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
        {
            TValue result;
            if (dictionary.TryGetValue(key, out result))
            {
                return result;
            }
            else
            {
                result = new TValue();
                dictionary.Add(key, result);
                return result;
            }
        }

        public static TValue GetOrReturnNew<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : new()
        {
            TValue result;
            if (dictionary.TryGetValue(key, out result))
            {
                return result;
            }
            else
            {
                return new TValue();
            }
        }

        public static TValue GetOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
        {
            TValue result;
            if (dictionary.TryGetValue(key, out result))
            {
                return result;
            }
            else
            {
                return default(TValue);
            }
        }
    }
}
